<?php

/**
 * @author Remi
 * @version 1.0
 */
class TypeDocument
{

    /**
     * @var int
     */
    private $IdType;
    /**
     * @var int
     */
    private $Deadline;
    /**
     * @var int
     */
    private $Promo;
    /**
     * @var string
     */
    private $NomType;

    /**
     *
     * @param int $id
     */
    public function __construct($id = 0)
    {

        if (empty($id))
        {
            $this->IdType = 0;
            $this->Deadline = 0;
            $this->Promo = 0;
            $this->NomType = "";

            $this->requeteOk = true;
            $this->errormsg = "";
        }
        else
        {
            //Instanciation connexion PDO
            $sql = SQL::getInstance()->getConnection();
            //On retourne l'entrée demandée
            $query = 'SELECT * FROM TypeDocument WHERE IdTypeDocument = :id';
            $c = $sql->prepare($query);
            $c->bindParam(':id', $id, PDO::PARAM_INT);
            //Exécution de la requête retourne false si echec
            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {
                    $this->IdType = $result->IdTypeDocument ;
                    $this->Deadline = $result->DateDeadlineRendu;
                    $this->Promo = $result->IdPromo;
                    $this->NomType = $result->NomTypeDocument ;
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Ce type de document n'existe pas !";
                }
            }
            else
            {
                //Récupération de l'erreur dans la variable de classe
                /*
                  Pour chaque new avec passage de paramètres vous pouvez savoir si vous avez bien un résultat, si false n'oubliez pas de géréz l'affichage du message,...
                 */
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    /**
     * Sauvegarde un Type de document dans la base
     */
    public function save()
    {
        if (isset($this->IdType) && !empty($this->IdType))
        {
            $sql = SQL::getInstance()->getConnection();
            $query =
                    'UPDATE TypeDocument
			SET
				 DateDeadlineRendu = :deadline,
				 IdPromo = :promo,
				 NomTypeDocument = :type
			WHERE
				IdTypeDocument 	= :id';
            $sql->beginTransaction();
            try
            {
                $c = $sql->prepare($query);

                $c->bindParam(':id', $this->IdType, PDO::PARAM_INT);
                $c->bindParam(":deadline", $this->Deadline, PDO::PARAM_INT);
                $c->bindParam(":promo", $this->Promo, PDO::PARAM_INT);
                $c->bindParam(":type", $this->NomType, PDO::PARAM_STR);


                if ($c->execute())
                {
                    $this->requeteOk = true;
                    $this->errormsg = "";
                    $sql->commit();
                    $c->closeCursor();
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = $c->errorInfo();
                    $sql->rollback();
                    $c->closeCursor();
                }
            }
            catch (PDOException $Exception)
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query =
                    'INSERT INTO TypeDocument
			(
				 DateDeadlineRendu,
				 IdPromo,
				 NomTypeDocument
			)
			VALUES
			(
				:deadline,
				:promo,
				:type
			)';
            $sql->beginTransaction();
            try
            {
                $c = $sql->prepare($query);

                $c->bindParam(":deadline", $this->Deadline, PDO::PARAM_INT);
                $c->bindParam(":promo", $this->Promo, PDO::PARAM_INT);
                $c->bindParam(":type", $this->NomType, PDO::PARAM_STR);

                if ($c->execute())
                {
                    $this->IdType = $sql->lastInsertId();
                    $sql->commit();
                    $c->closeCursor();
                    $this->requeteOk = true;
                    $this->errormsg = "";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = $c->errorInfo();
                    $sql->rollback();
                    $c->closeCursor();
                    $this->IdType = 0;
                }
            }
            catch (PDOException $Exception)
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdType = 0;
            }
        }
    }

    /**
     * Supprime un type de document de la base
     */
    public function delete()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = 'DELETE FROM TypeDocument WHERE IdTypeDocument = :id';
        $c = $sql->prepare($query);
        $c->bindParam(':id', $this->IdType, PDO::PARAM_INT);
        $c->execute();
    }

    /**
     * @return int
     */
    public function getIdType()
    {
        return $this->IdType;
    }

    /**
     * @return int
     */
    public function getDeadline()
    {
        return $this->Deadline;
    }

    /**
     * @param int $Deadline
     * @return array
     */
    public function setDeadline($Deadline)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        if (isset($Deadline) && !empty($Deadline) && strlen($Deadline) == 10)
        {
            $this->Deadline = $Deadline;
            $this->errormsg = "";
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "La date est mal défini";
            $this->Deadline = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getPromo()
    {
        return $this->Promo;
    }

    /**
     *
     * @param int $Promo
     * @return array
     */
    public function setPromo($Promo)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        if (isset($Promo) && !empty($Promo))
        {
            $this->Promo = $Promo;
            $this->errormsg = "";
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Le Type doit etre defini !";
            $this->Promo = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return string
     */
    public function getNomType()
    {
        return $this->NomType;
    }

    /**
     *
     * @param string $NomType
     * @return array
     */
    public function setNomType($NomType)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        if (isset($NomType) && !empty($NomType) && strlen($NomType) < 255)
        {
            $this->NomType = htmlentities($NomType);
            $this->errormsg = "";
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Lne nom n'est pas bien defini !";
            $this->NomType = 0;
        }
        return $errTab;
    }

    /**
     * Renvoie la liste des documents obligatoires
     * @static
     * @return mixed Array de documents obligatoires
     */
    static public function getListTypeDocument($byPromo = 0){
        
        if (empty($byPromo)) {
            $sql = SQL::getInstance()->getConnection();

            $query = "SELECT * FROM TypeDocument ORDER BY IdPromo asc, DateDeadlineRendu asc";
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);

            if (!empty($result))
            {
                return $result;
            }
            else {
                return array();
            }
        }
        else {
           $sql = SQL::getInstance()->getConnection();

            $query = "SELECT * FROM TypeDocument where TypeDocument.IdPromo = :byPromo ORDER BY IdPromo asc, DateDeadlineRendu asc";
            $c = $sql->prepare($query);
            $c->bindParam(':byPromo', $byPromo, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);

            if (!empty($result))
            {
                return $result;
            }
            else {
                return array();
            }
        }
    }
	
	/**
	 *Renvoie la liste des docuements obligatoires pour une promo donnée
	 *@static
	 *@return mixed Array de documents obligatoires par promo mais ne prend pas les types documents contenus dans la table RequiredDocument qui eux doivent être gérés à la main
	 */ 
	static public function getListTypeDocumentByPromo($idPromo){
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM TypeDocument td WHERE td.IdPromo = :idPromo";
		$c = $sql->prepare($query);
        $c->bindParam(':idPromo', $idPromo, PDO::PARAM_INT);
		$c->execute();
        $result = $c->fetchAll(PDO::FETCH_OBJ);
		
        if (!empty($result))
        {
            return $result;
        }
        return array();
	}
}

?>